var app = getApp();
Page({
  data: {
    oid: '',
    vertical: false,

    mc: '',//辅导班名称
    fy: 0,//辅导班的费用
    priarr: [],//被选中的收费
    teas: [],//教师列表
    allprice: 0,//总价格

    prepay_id: '',//交易id
    wxid: '',

    fs1state:'',//付款方式1  支付到个人
    fs2state: '',//付款方式2 支付到银行账号
    fs3state: '',//微信支付

    fs1: '',//付款方式1  支付到个人
    fs2: '',//付款方式2 支付到银行账号
    fs3: '',//微信支付
   
  },

  onLoad: function (options) {
    var that = this;
    var oid = options.oid;
    if (oid != '' && oid != null && oid != undefined && oid != 'undefined') {
      that.setData({
        oid: oid
      })
    }

    var openid = wx.getStorageSync('openid');
    if (openid == "") {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } else {
      that.setData({
        wxid: openid
      })
      that.getdata();
      that.getJffs();
    }
  },

  getdata: function () {//定义函数名称
    var that = this;
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/orderWhk_list.html',//请求地址
      data: {//发送给后台的数据
        oid: that.data.oid
      },
      header: {//请求头
        "Content-Type": "applciation/json"
      },
      method: "GET",//get为默认方法/POST
      success: function (res) {
        that.setData({
          mc: res.data.MC,
          jj: res.data.JJ,
          priarr: res.data.pris,
          teas: res.data.teas,
          fy: res.data.FY,
          allprice: res.data.ALLPRICE,
        });
        wx.hideLoading()
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },



  getJffs: function () {//定义函数名称
    var that = this;
    var csid = wx.getStorageSync('csid');
    wx.request({
      url: app.myUlife.ulifeUrl + '/jffs/getJffsBycsid.html',//请求地址
      data: {//发送给后台的数据
        csid: csid
      },
      header: {//请求头
        "Content-Type": "applciation/json"
      },
      method: "GET",//get为默认方法/POST
      success: function (res) {
       
        if (res.data.FS1STATE){
          that.setData({
            fs1state: res.data.FS1STATE,
          });
        }
        if (res.data.FS2STATE) {
          that.setData({
            fs2state: res.data.FS2STATE,
          });
        }
        if (res.data.FS3STATE) {
          that.setData({
            fs3state: res.data.FS3STATE,
          });
        }
        if (res.data.FS1) {
          that.setData({
            fs1: res.data.FS1,
          });
        }
        if (res.data.FS2) {
          that.setData({
            fs2: res.data.FS2,
          });
        }
        if (res.data.FS3) {
          that.setData({
            fs3: res.data.FS3,
          });
        }
       
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },


  gotoPay: function () {
    var that = this;
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/submitorder.html',
      data: {
        oid: that.data.oid,
        ordername: that.data.mc,
        wxid: that.data.wxid,
        allprice: that.data.allprice,
        fs3: that.data.fs3,//手续费
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "get",//get为默认方法/POST
      success: function (res) {
        var timeStamp = res.data.timeStamp;
        var nonceStr = res.data.nonceStr;
        var prepay_id = res.data.prepay_id;
        that.setData({
          prepay_id: prepay_id
        })
        var sign = res.data.sign;
        wx.requestPayment(
          {
            'timeStamp': timeStamp,
            'nonceStr': nonceStr,
            'package': 'prepay_id=' + prepay_id,
            'signType': 'MD5',
            'paySign': sign,
            'success': function (res) {
              console.info("支付成功");
              //修改订单为已经支付
              that.oradePay();
            },
            'fail': function (res) { },
            'complete': function (res) {
              console.info("支付完成");

            }
          })
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  }, 



  oradePay: function (e) {
    var that = this;
    var oid = that.data.oid;//订单id
    if (oid == "") {
      return;
    }
    var prepay_id = that.data.prepay_id;
    if (prepay_id == "") {
      return;
    }
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/paysuccess.html',
      data: {
        oid: oid,
        prepay_id: prepay_id
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "get",//get为默认方法/POST
      success: function (res) {
        //跳转到成功页面
        wx.reLaunch({
          url: '../../pages/paysuccess/paysuccess?oid=' + oid
        })
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },


})
